package com.zut.dao;

import com.zut.domain.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface PostDao {


    @Select("select post_id as postId,post.user_id as userId," +
            "if(post_user_type = '学员',user_name,coach_name) as name," +
            "if(post_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(post_user_type = '学员',user_img,coach_img) as img," +
            "post_title as title,post_date as date,post_type as subject," +
            "post_user_type as type," +
            "post_content as content," +
            "post_click as click from post " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id")
    List<Map<String, String>> getAll();

    @Insert("insert into post (post_id,user_id,post_title,post_content,post_date,post_click,post_type,post_user_type) " +
            "values(#{postId},#{userId},#{title},#{content},#{date},#{click},#{subject},#{type})")
    int insert(Post post);

    @Delete("delete from post where post_id = #{id}")
    int deleteById(String id);

    @Update("<script>                                                       "
            + "update post                                                  "
            + "<set>                                                        "
            + "  <if test='title != null'> post_title=#{title},        </if>"
            + "  <if test='content != null'> post_content=#{content},  </if>"
            + "  <if test='date != null'> test_date=#{date},           </if>"
            + "  <if test='click != null'> test_click=#{click},           </if>"
            + "  <if test='subject != null'> post_type=#{subject},           </if>"
            + "  <if test='type != null'> post_user_type=#{type},           </if>"
            + "</set>                                                       "
            + "where post_id = #{id}                                        "
            + "</script>                                                    ")
    int update(Post post);

    @Select("select post_id as postId,post.user_id as userId," +
            "if(post_user_type = '学员',user_name,coach_name) as name," +
            "if(post_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(post_user_type = '学员',user_img,coach_img) as img," +
            "post_title as title,post_date as date,post_type as subject," +
            "post_user_type as type," +
            "post_content as content," +
            "post_click as click from post " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id " +
            "where post_type = #{subject}")
    List<Map<String, String>> getAllBySubject(String subject);

    @Select("select post_id as postId,post.user_id as userId," +
            "if(post_user_type = '学员',user_name,coach_name) as name," +
            "if(post_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(post_user_type = '学员',user_img,coach_img) as img," +
            "post_title as title,post_date as date,post_type as subject," +
            "post_user_type as type," +
            "post_content as content," +
            "post_click as click from post " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id " +
            "order by post.post_date desc")
    List<Map<String, String>> getAllByTime();

    @Select("select post_id as postId,post.user_id as userId," +
            "if(post_user_type = '学员',user_name,coach_name) as name," +
            "if(post_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(post_user_type = '学员',user_img,coach_img) as img," +
            "post_title as title,post_date as date,post_type as subject," +
            "post_user_type as type," +
            "post_content as content," +
            "post_click as click from post " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id " +
            "order by post.post_click desc")
    List<Map<String, String>> getAllByClick();

    @Update("update post set post_click = post_click + 1 where post_id = #{id}")
    int updateClick(String id);

    @Select("select post_id as postId,post.user_id as userId," +
            "if(post_user_type = '学员',user_name,coach_name) as name," +
            "if(post_user_type = '学员',user_sex,coach_sex) as sex," +
            "if(post_user_type = '学员',user_img,coach_img) as img," +
            "post_title as title,post_date as date,post_type as subject," +
            "post_user_type as type," +
            "post_content as content," +
            "post_click as click from post " +
            "left join student on post.user_id = student.user_id " +
            "left join coach on post.user_id = coach.coach_id " +
            "where post_id = #{id}")
    Map<String, String> getById(String id);

    @Select("select count(distinct post.post_id) as postNum," +
            "count(distinct reply.reply_id) as replyNum," +
            " count(fond.reply_id) as likeNum " +
            "from student " +
            "left join reply on student.user_id = reply.reply_user_id " +
            "left join fond on reply.reply_id = fond.reply_id " +
            "left join post on student.user_id = post.user_id " +
            "where student.user_id = #{id}")
    Map<String, String> getStudentMessage(String id);

    @Select("select count(distinct post.post_id) as postNum," +
            "count(distinct reply.reply_id) as replyNum," +
            " count(fond.reply_id) as likeNum " +
            "from coach " +
            "left join reply on coach.coach_id = reply.reply_user_id " +
            "left join fond on reply.reply_id = fond.reply_id " +
            "left join post on coach.coach_id = post.user_id " +
            "where coach.coach_id = #{id}")
    Map<String, String> getCoachMessage(String id);

    @Select("select post_id as postId," +
            "post_title as title," +
            "post_content as content," +
            "post_date as date," +
            "post_click as click," +
            "post_type as subject " +
            "from post " +
            "where user_id = #{id}")
    List<Map<String, String>> getPostByUserId(String id);
}
